﻿Imports Infragistics.Web.UI.NavigationControls
Imports Infragistics.Web.UI

Imports System.Data.SqlClient
Imports Infragistics.Web.UI.GridControls
Imports Infragistics.Web.UI.DataSourceControls



Public Class Awards
    Inherits System.Web.UI.Page
    Private et As New ETModelContainer
    Private table As New HtmlTable
    Private IsEditor As Boolean = False
    Private Sub Awards_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        AddHandler CType(Page.Master, IMasterPage).OnScoutChanged, AddressOf OnScoutChanged
        If Not IsPostBack Then

            LoadAwards()


        End If



        CreateTable()

    End Sub

    Public Sub OnScoutChanged()
        LoadAwards()
        CreateTable()
    End Sub


    Private Sub LoadAwards()

        Dim orgid As Integer = CInt(CType(Page.Master, IMasterPage).CurrentScout.OrganizationID)
        Dim ar = (From a In et.OrgReqs
          Join b In et.AwardRequirements On a.RequirementID Equals b.RequirementID
          Where a.OrganizationID = orgid Select b)


        IsEditor = CType(Me.Master, IMasterPage).CurrentUser.IsInRole(UNIT_AWARDS_EDITOR, orgid, et)


        cboAwards.DataSource = ar
        cboAwards.DataTextField = "Requirement"
        cboAwards.DataValueField = "RequirementID"
        cboAwards.DataBind()
    End Sub


    Private Sub cboAwards_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles cboAwards.SelectedIndexChanged


        CreateTable()


    End Sub

    Private Sub CreateTable()
        ph.Controls.Clear()



        If cboAwards.SelectedValue.Length > 0 Then
            table = MakeBody(CInt(cboAwards.SelectedValue))
            If Not table Is Nothing Then
                table.Border = 1

                table.Width = "100%"
                table.Attributes.Add("RunAt", "Server")
                ph.Controls.Add(table)
            End If
        End If
    End Sub
    Private Sub MakeHeader(toTable As HtmlTable)
        Dim dt As DataTable = MakeDataTable(0)

        Dim theadrow As New HtmlTableRow
        For Each col As DataColumn In dt.Columns
            Select col.ColumnName

                Case "ParentID", "RequirementID", "ChildrenRequired", "Description", "ScoutID"

                Case Else
                    Dim tcell As New HtmlTableCell
                    tcell.InnerText = col.ColumnName.Substring(col.ColumnName.IndexOf("_") + 1)

                    If IsRootComplete() Then
                        tcell.BgColor = "Yellow"
                    End If


                    theadrow.Cells.Add(tcell)
            End Select
        Next
        toTable.Rows.Add(theadrow)

    End Sub
    Private Function IsRootComplete() As Boolean

        Dim compcount As Integer = (From comp In et.Completeds Where comp.RequirementID = CInt(cboAwards.SelectedValue) And comp.ScoutID = CType(Page.Master, IMasterPage).CurrentScoutID).Count

        If compcount > 0 Then
            Return True
        Else
            Return False
        End If



    End Function

     
    Private Function MakeBody(reqid As Integer) As HtmlTable

        Dim lTab As New HtmlTable
        lTab.Border = 1
        lTab.Width = "75%"
        Dim dtab As DataTable = MakeDataTable(reqid)
        If dtab.Rows.Count > 0 Then
            MakeHeader(lTab)



            For Each row As DataRow In dtab.Rows

                'Check if row has child by calling down and getting the table
                Dim ctab As HtmlTable = MakeBody(CInt(row("RequirementID")))
                If Not ctab Is Nothing Then
                    ctab.Attributes.Add("RunAt", "Server")
                End If
                Dim htmlrow As New HtmlTableRow
                For ix As Integer = 0 To dtab.Columns.Count - 1
                    Dim htmlcell As New HtmlTableCell
                    Select Case dtab.Columns(ix).ColumnName
                        Case "ParentID", "RequirementID", "ChildrenRequired", "Description", "ScoutID"

                        Case "Requirement"
                            htmlcell.InnerText = row(ix).ToString

                            htmlrow.Cells.Add(htmlcell)
                        Case "CompletedOn"
                            If ctab Is Nothing Then
                                Dim dat As New Infragistics.Web.UI.EditorControls.WebDateTimeEditor
                                dat.AutoPostBackFlags.ValueChanged = AutoPostBackFlag.On
                                dat.Value = row(ix).ToString
                                dat.ID = row("RequirementID").ToString
                                AddHandler dat.ValueChanged, AddressOf DateValueChanged


                                dat.ReadOnly = Not IsEditor


                                htmlcell.Controls.Add(dat)
                            ElseIf row(ix).ToString.Length > 0 Then
                                htmlcell.InnerHtml = CDate(row(ix).ToString).ToShortDateString
                                '   htmlcell.Attributes.Add("class", "textEntry")
                                htmlcell.BgColor = "Yellow"
                            End If


                            htmlrow.Cells.Add(htmlcell)

                    End Select
                Next
                lTab.Rows.Add(htmlrow)

                'if the table comes back nothing then move on

                'if the table has rows then add a row and nest the table
                If Not ctab Is Nothing Then
                    Dim drow As New HtmlTableRow
                    Dim dcell As New HtmlTableCell
                    dcell.ColSpan = dtab.Columns.Count
                    dcell.Align = "Center"
                    dcell.Controls.Add(ctab)
                    drow.Cells.Add(dcell)
                    lTab.Rows.Add(drow)

                End If
            Next



        Else
            Return Nothing

        End If
        Return lTab

    End Function

    Private Sub DateValueChanged(sender As Object, e As EditorControls.TextEditorValueChangedEventArgs)
   
        Dim reqID As Integer = CInt(CType(sender, Control).ID)
        Dim ScoutID As Integer = CType(Page.Master, IMasterPage).CurrentScoutID
        'Update Completed status here and update tree in SQL SP

        et.CompleteReg(ScoutID, CDate(e.NewValue), reqID)

        CreateTable()
    End Sub
    Private Function MakeDataTable(CatagoryID As Integer) As DataTable

        Dim dt As New DataTable

        Using conn As SqlConnection = New SqlConnection(My.Settings.AltDBConn)
            Using comm As New SqlCommand("EXEC GetRequirementsPerScout " & CatagoryID & ",'" & CType(Page.Master, IMasterPage).CurrentScoutID & "'", conn)

                conn.Open()

                Dim adp As New SqlDataAdapter(comm)
                adp.Fill(dt)

                conn.Close()
            End Using

        End Using

        Return dt

    End Function

 




End Class